package com.cennavi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/** * @author  作者 :allen
    * @date 创建时间：2017年9月20日 上午8:54:13 
    * @version 1.0 
    */

public class DateUtil {

	/**
	 * 比较两个时间戳早晚，前比后早返回true
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compTime(Date date1, Date date2){
		return date1.getTime() < date2.getTime() ? true : false;
	}
	
	
	/**
	 * 比较时间是否相差小于多少分钟
	 * @param date1
	 * @param date2
	 * @param minutes
	 * @return
	 */
	public static boolean compareTime(Date earlierDate, Date laterDate, int minutes){
		return (calMinutes(earlierDate, laterDate) < minutes) ? true : false;
	}
	
	/**
	 * 获取分钟差
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long calMinutes(Date date1, Date date2){
		return (date2.getTime() - date1.getTime()) / (1000 * 60);
	}
	
	/**
	 * 把当前时间划归到整15分钟
	 * @param date 输入时间
	 * @return 输出时间精确到分钟
	 */
	public static String get15min(Date date){
		String time2match = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		String time2return = "";
		int minute = Integer.parseInt(time2match.substring(14,16));
		if(minute >= 0 && minute < 15){
			time2return = time2match.substring(0,14) + "15";
		}else if(minute >= 15 && minute < 30){
			time2return = time2match.substring(0,14) + "30";
		}else if(minute >= 30 && minute < 45){
			time2return = time2match.substring(0,14) + "45";
		}else{
			//最后一种情况比较特殊，需要进位
			//确定的是，分钟数肯定是00，但是小时甚至日期都涉及到进位，只能通过时间来操作
			String tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date).substring(0, 14) + "59:00";
			Date tempDate;
			try {
				tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(tempTime);
				//时间加一分钟，系统自己来进位
				tempDate = new Date(tempDate.getTime() + 60000);
				time2return = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(tempDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return time2return;
	}
	
	/**
	 * 把当前时间划归到整5分钟(向后取整)
	 * @param date 输入时间
	 * @return 输出时间精确到分钟
	 */
	public static String get5min_(Date date){
		String time2match = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		String time2return = "";
		int minute = Integer.parseInt(time2match.substring(14,16));
		if(minute >= 0 && minute < 5){
			time2return = time2match.substring(0,14) + "05";
		}else if(minute >= 5 && minute < 10){
			time2return = time2match.substring(0,14) + "10";
		}else if(minute >= 10 && minute < 15){
			time2return = time2match.substring(0,14) + "15";
		}else if(minute >= 15 && minute < 20){
			time2return = time2match.substring(0,14) + "20";
		}else if(minute >= 20 && minute < 25){
			time2return = time2match.substring(0,14) + "25";
		}else if(minute >= 25 && minute < 30){
			time2return = time2match.substring(0,14) + "30";
		}else if(minute >= 30 && minute < 35){
			time2return = time2match.substring(0,14) + "35";
		}else if(minute >= 35 && minute < 40){
			time2return = time2match.substring(0,14) + "40";
		}else if(minute >= 40 && minute < 45){
			time2return = time2match.substring(0,14) + "45";
		}else if(minute >= 45 && minute < 50){
			time2return = time2match.substring(0,14) + "50";
		}else if(minute >= 50 && minute < 55){
			time2return = time2match.substring(0,14) + "55";
		}else{
			//最后一种情况比较特殊，需要进位
			//确定的是，分钟数肯定是00，但是小时甚至日期都涉及到进位，只能通过时间来操作
			String tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date).substring(0, 14) + "59:00";
			Date tempDate;
			try {
				tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(tempTime);
				//时间加一分钟，系统自己来进位
				tempDate = new Date(tempDate.getTime() + 60000);
				time2return = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(tempDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return time2return;
	}
	
	/**
	 * 把当前时间划归到整5分钟(向前取整)
	 * @param date 输入时间
	 * @return 输出时间精确到分钟
	 */
	public static String get5min(Date date){
		String time2match = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		String time2return = "";
		int minute = Integer.parseInt(time2match.substring(14,16));
		if(minute >= 0 && minute < 5){
			time2return = time2match.substring(0,14) + "00";
		}else if(minute >= 5 && minute < 10){
			time2return = time2match.substring(0,14) + "05";
		}else if(minute >= 10 && minute < 15){
			time2return = time2match.substring(0,14) + "10";
		}else if(minute >= 15 && minute < 20){
			time2return = time2match.substring(0,14) + "15";
		}else if(minute >= 20 && minute < 25){
			time2return = time2match.substring(0,14) + "20";
		}else if(minute >= 25 && minute < 30){
			time2return = time2match.substring(0,14) + "25";
		}else if(minute >= 30 && minute < 35){
			time2return = time2match.substring(0,14) + "30";
		}else if(minute >= 35 && minute < 40){
			time2return = time2match.substring(0,14) + "35";
		}else if(minute >= 40 && minute < 45){
			time2return = time2match.substring(0,14) + "40";
		}else if(minute >= 45 && minute < 50){
			time2return = time2match.substring(0,14) + "45";
		}else if(minute >= 50 && minute < 55){
			time2return = time2match.substring(0,14) + "50";
		}else{
			time2return = time2match.substring(0,14) + "55";
		}
		return time2return;
	}
	
	/**
	 * 
	 * @param timestamp 时间戳格式必须是yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static boolean isIntegral5min(String timestamp){
		String time = timestamp.substring(10,12);
		return Integer.parseInt(time) % 5 == 0;
	}
	
	public static void main(String[] args) throws ParseException {
		String timestamp = "2017-09-20 23:01:55";
		Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timestamp);
		System.out.println(get5min(date));
//		System.out.println(isIntegral5min("2017-09-20 16:21"));
	}
}
